package com.cloud.pole.rpc.account;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.api.user.UserAccountApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.user.model.entity.account.UserAccount;
import com.cloud.pole.user.service.AuthService;
import com.cloud.pole.user.service.UserService;
import com.cloud.pole.user.service.account.UserAccountDetailService;
import com.cloud.pole.user.service.account.UserAccountService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/userAccount")
public class UserAccountRest {
	
	@Autowired
	UserAccountService userAccountService;
	
	/**
	 * 增加余额
	 * @param typeId 账目类型ID
	 * @param userId
	 * @param userName
	 * @param objectId 对象id
	 * @param objectTypeId 对象类型Id
	 * @param amount
	 * @param srcUserId
	 * @param srcUserName
	 * @param srcRealName
	 * @param note
	 * @return
	 */
    @RequestMapping(value = "/addBalance", method = RequestMethod.GET)
    public ApiRestResponse<Integer> addBalance(Integer typeId, Long userId, String userName, String objectId, Integer objectTypeId, BigDecimal amount, 
    		Long srcUserId, String srcUserName, String srcRealName, String note){   	
    	try {
			ParamException.err(StringUtils.isBlank(userName), "userName不能为空");
			ParamException.err(StringUtils.isBlank(objectId), "objectId不能为空");
			
			int status = userAccountService.handleBalance(typeId, userId, userName, objectId, objectTypeId, amount, 
					srcUserId, srcUserName, srcRealName, note);

			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, status);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
	/**
	 * 减少余额
	 * @param typeId 账目类型ID
	 * @param userId
	 * @param userName
	 * @param objectId 对象id
	 * @param objectTypeId 对象类型Id
	 * @param amount
	 * @param srcUserId
	 * @param srcUserName
	 * @param srcRealName
	 * @param note
	 * @return
	 */
    @RequestMapping(value = "/reduceBalance", method = RequestMethod.GET)
    public ApiRestResponse<Integer> reduceBalance(Integer typeId, Long userId, String userName, String objectId, Integer objectTypeId, BigDecimal amount, 
    		Long srcUserId, String srcUserName, String srcRealName, String note){   	
    	try {
			ParamException.err(StringUtils.isBlank(userName), "userName不能为空");
			ParamException.err(StringUtils.isBlank(objectId), "objectId不能为空");
			
			int status = userAccountService.handleBalance(typeId, userId, userName, objectId, objectTypeId, amount, 
					srcUserId, srcUserName, srcRealName, note);

			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, status);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "/getAccountList", method = RequestMethod.GET)
    public ApiRestResponse<List<UserAccountApi>> getAccountList(String token){   	
    	try {
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userAccountService.getAccountList(token));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }    
    
    @RequestMapping(value = "/getAccountTypeName", method = RequestMethod.GET)
    public ApiRestResponse<String> getAccountTypeName(Integer typeId){   	
    	try {
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userAccountService.getAccountTypeName(typeId));
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }    
    
}
